<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>
    <button id="next">下一页</button>
  </div>
  <h3 id="urlList">
    <!-- 1.所有商品 -->
    <!-- 1.1 第一页 展示索引 -->
    <!-- <div class='item'>https://sxyrzy.en.alibaba.com/productlist-1.html?filterSimilar=true&scene=rts&filter=all&sortType=ctrOrder-desc</div> -->
    <!-- 1.2 第一页 不展示索引 -->
    <!-- <div class='item'>https://dgyihao.en.alibaba.com/productlist.html?spm=a2700.shop_pl.88.6</div> -->
    
    <!-- 2.带类目的商品 -->
    <!-- 2.1 第一页 展示索引 -->
    <!-- <div class='item'>https://dgyihao.en.alibaba.com/productgrouplist-803784389-1/Womens_Dresses.html?spm=a2700.shop_pl.98.1</div> -->
    <!-- 2.2 第一页 不展示索引 -->
    <div class='item'>https://dgyihao.en.alibaba.com/productgrouplist-803784389/Womens_Dresses.html?spm=a2700.shop_pl.98.1</div>
  </h3>
</body>
<script>
  let urlWrapper = document.getElementById('urlList');
  const nextBtn = document.getElementById('next');

  nextBtn.addEventListener('click', () => {
    let urlDomList = document.getElementsByClassName('item');
    urlDom = urlDomList[urlDomList.length - 1];
    let url = urlDom.textContent, curPage, nextPage, finalUrl;

    if (url.includes("productlist")) {
      let regAllProductHavePage = new RegExp(/\/productlist-(.*?)\.html/g);
      if (regAllProductHavePage.test(url)) {
        finalUrl = url.replace(regAllProductHavePage, (allStr, match) => {
          curPage = match;
          nextPage = parseInt(match) + 1;
          return allStr.replace(match, nextPage);
        });
      } else {
        curPage = 1;
        nextPage = 2;
        finalUrl = url.replace("productlist", "productlist-2");
      }
    } else if(url.includes("productgrouplist")) {
      let regCategoryProductHavePage = new RegExp(/\/productgrouplist-(.*?)-(\d*)/g);
      let regCategoryProductNoPage = new RegExp(/\/productgrouplist-(\d*)/g);
      if(regCategoryProductHavePage.test(url)) {
        finalUrl = url.replace(regCategoryProductHavePage, (allStr, match1, match2) => {
          const tempArr = allStr.split("-");
          curPage = parseInt(tempArr[tempArr.length - 1]);
          nextPage = curPage + 1
          tempArr[tempArr.length - 1] = nextPage;
          return tempArr.join("-");
        });
      } else {
        curPage = 1;
        nextPage = 2;
        finalUrl = url.replace(regCategoryProductNoPage, (allStr, match) => {
          return allStr + '-2';
        });
      }
    }

    console.log("本页", curPage, "下一页", nextPage);

    // 创建dom
    const newDom = document.createElement('div');
    newDom.setAttribute('class', 'item');
    newDom.textContent = finalUrl;
    urlWrapper.appendChild(newDom);
  });
</script>

</html>